home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
pascal
/
tpref.exe
/
TPR3C.TXT
< prev
next >
Wrap
Text File
|
1992-10-19
|
61KB
|
1,498 lines
Chapter 3
- continued -
- Part 3 of 3 parts -
of the
Turbo Pascal Reference
The Turbo Pascal Language
This chapter is part of the Turbo Pascal Reference electronic freeware book (C)
Copyright 1992 by Ed Mitchell. This freeware book contains supplementary
material to Borland Pascal Developer's Guide, published by Que Corporation,
1992. However, Que Corporation has no affiliation with nor responsibility for
the content of this free book. Please see Chapter 1 of the Turbo Pascal
Reference for important information about your right to distribute and use this
material freely. If you find this material of use, I would appreciate your
purchase of one my books, such as the Borland Pascal Developer's Guide or
Secrets of the Borland C++ Masters, Sams Books, 1992. Thank you.
Functions
Syntax:
function <identifier> <parameter list> : <data type>
Examples:
function Minimum ( A, B : Integer ) : Integer;
{ Returns the value of the smaller number A, or B }
begin
if A < B then
Minimum := A
else { if B < A or B = A then }
Minimum := B;
end;
{ Example of a function returning a String value }
function LowerCase (S : String ) : String;
{ Convert string S to lower case, returning the result }
var
I : Integer;
begin
for I := 1 to length(S) do
if ((S[I]>='A') and (S[I]<='Z')) then
S[I] := Chr( Ord( S[I] ) + 32 );
LowerCase := S;
end; { LowerCase }
Description:
A function is similar to a procedure, except that a function is called
from within an expression and it returns a value that is then used in
evaluating the overall expression. Functions can have both value and variable
parameters, and may be declared as near, far, forward, and external, or may be
implemented entirely in assembly language. Functions may not, however, be
declared as interrupt functions.
Calling a function
The function is called by appearing within an expression, such as,
I := Min ( X1, X2 ) + 1;
Assigning a value to a function identifier
The function's block definition must include a statement that assigns a
value to the function's identifier. This is how a function gets a value that
it can return to the caller. If you omit this assignment statement, or the
assignment statement does not get executed as a result of conditional
statements in the function's code, then the function returns an undefined or
potentially random value. If during the course of debugging a program you find
your functions returning erratic values, be certain that the function
identifier is correctly assigned a value.
Examples of assigning values to function identifiers appear above in the
Min and LowerCase functions.
Acceptable Function Return Values
The data type that a function returns can be any of the following:
Any ordinal value, including Boolean, Byte, Char, Smallint, Integer,
Word, Longint, and enumarated data types and user defined sub range
types.
Real, Single, Double, Extended and Comp data types,
Pointer values
Strings.
Functions may not return records or sets, although they may return pointers to
records or sets.
Recursive functions
Functions may call themselves. Such a function is called a recursive
function. A popular and simple example of a recursive function is a function
that computes the factorial of a number. The factorial of a number n, is n *
(n-1) * (n-2) ... until n reaches 1. For example, the factorial of 5, is 5 * 4
* 3 * 2 * 1, which equals 120. An illustration of how this might be solved
recursively is shown in listing 3.10. (Actually, its not necessary to use a
recursive function to compute a factorial; this method is used here for
illustration only.)
Listing 3.10. An example of a recursive function.
1 program DemoRecursion; {DEMORECU.PAS}
2
3 function Factorial ( n : real ) : real;
4 begin
5 if n = 1 then
6 Factorial := 1
7 else
8 Factorial := N * Factorial ( N - 1.0 );
9 end;
10
11 var
12 X : Real;
13
14 begin
15 Write('Enter a number: ');
16 Readln( X );
17 Writeln;
18 Writeln('Factorial of ',X,' = ', Factorial ( X ) );
19 Writeln;
20 Write('Press Enter to finish.');
21 Readln;
22 end.
23
Important note: The effect of short-circuit evaluation on functions
By default, Turbo Pascal generates short-circuit evaluation code, so it is
possible that a function may not be called within a particular expression. For
example, consider a function defined as:
function ValueInRange ( X1 : Integer ) : Boolean;
begin
...
if X1 > 0 then
ValueInRange := True
else
ValueInRange := False;
if X1 < LowestCoordinate then
LowestCoordinate := X1;
end;
In this function, a global variable LowestCoordinate may have its value changed
during the course of execution. If this function is called in an expression
such as,
if (X1<>X2) and ValueInRange(X1) then ...
In normal short-circuit evaluation, if X1 is not equal to X2, then the
remainder of the expression will not be evaluated. If your code depends upon
the value of LowestCoordinate being set as a side effect of the ValueInRange
function call, this may result in an error. In general, it is best to avoid
side-effects within functions and procedures, but if you must make use of
side-effect such as this, you should disable short-circuit evaluation to force
the entire expression to be fully evaluated. See the section "Compiler
Directives" for more information on using the {$B+} option to enable full
expression evaluation.
Procedures and Functions as Parameters
A procedure or function may itself be passed to another procedure or
function as a parameter value. To pass a procedure or function as a parameter
requires that a type declaration define a procedure type that matches the
appropriate procedure or function header. This type becomes the parameter type
used in the procedure parameter list. Listng 3.11 demonstrates the use of a
procedure parameter.
Note that the type declaration describes the procedure or function that is
called, and does not include the actual procedure or function identifier.
Listing 3.11. Using a procedure type as a procedure parameter.
1 Program ProcParameter;{PROCPARM.PAS}
2 { Demonstrates using a procedure type as a procedure
parameter }
3
4 type
5 FormatProc = procedure ( X : Integer );
6
7
8 const
9 MaxListSize = 15;
10 Values: array[1..MaxListSize] of Integer =
11 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
12
13
14
15 function Hexadecimal ( X: Word ) : String;
16 var
17 HiByte, LoByte : Word;
18
19 function HexConvert( B : Byte ) : String;
20 const
21 HexTable : Array[0..15] of Char =
22 ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
23 'A', 'B', 'C', 'D', 'E', 'F');
24 begin
25 HexConvert := HexTable[B div 16] + HexTable[B and 15];
26 end;
27
28 begin
29 HiByte := X Div 256;
30 LoByte := X and 255;
31 Hexadecimal := HexConvert( HiByte ) + HexConvert(LoByte);
32 end;
33
34
35
36 procedure PrintInteger( X : Integer ); far;
37 begin
38
39 Writeln( X : 5 );
40
41 end;
42
43
44
45 procedure PrintHex( X : Integer ); far;
46 begin
47
48 Writeln ( Hexadecimal ( X ) );
49
50
51 end;
52
53
54 procedure PrintPercent( X : Integer ); far;
55 begin
56
57 Writeln( X, '%');
58
59 end;
60
61
62 procedure Traverse ( Proc : FormatProc );
63 var
64